<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<base href="<%=basePath%>"/>
	<body >
	<link rel="stylesheet" type="text/css" href="<c:url value='/resource/css/zTreeStyle/zTreeStyle.css'/>"></link>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery-1.7.2.min.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.core-3.2.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.excheck-3.2.min.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/artdialog/artdialog.js?skin=${sys_theme}'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/common.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/artdialog/iframetools.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/map.js'/>"></script>
	<script type="text/javascript">
		var setting = {
			async : {
				enable : true,
				url : "<c:url value='/dept/getAllDept.json'/>",
				dataType : "json",
				type : 'get',
				autoParam : [ "deptId" ],
				dataFilter : function(treeId, parentNode, childNodes) {
					return childNodes.data
				}
			},
			callback : {
				beforeExpand: beforeExpand,
				onExpand: onExpand,
				onAsyncSuccess:onAsyncSuccess
			},
			check:{
				enable:true
			},
			data : {
				key:{
					name:"alias"
				},
				simpleData : {
					enable : true,
					idKey : "deptId",
					pIdKey : "parentId",
					rootPId : 0
				}
			},
			edit:{
				enable:true,
				showRemoveBtn:false,
				showRenameBtn:false
			}
		};
		
		function onAsyncSuccess(event,treeId,treeNode,msg){
			if(treeNode!==undefined){
				var zTree = $.fn.zTree.getZTreeObj(treeId);
				var type= zTree.setting.check.chkboxType;
				if(type.N=="ps"||type.N=="s"){
					//子关联
					if(treeNode.checked){
						$.each(treeNode.children,function(i,node){
							//node.checked=true;
							zTree.checkNode(node,true);
						})
					}
				}
			}
		}
		/**树初始化*/
		$(function() {
			$.fn.zTree.init($("#treeDemo"), setting);
			
			//绑定事件是否父子关联
			$("#py").bind("change",setCheck);
			$("#sy").bind("change",setCheck);
		});
		
		function setCheck(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.checkAllNodes(false);
			var p =$("#py").attr("checked")?"p":""; 
			var s =$("#sy").attr("checked")?"s":""; 
			var type={"Y":p+s,"N":p+s};
			zTree.setting.check.chkboxType=type;
		}
		
		/**同时只能展开一个节点，减少树的长度 start*/
		var curExpandNode = null;
		function beforeExpand(treeId, treeNode) {
			var pNode = curExpandNode ? curExpandNode.getParentNode():null;
			var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
				if (treeNode !== treeNodeP.children[i]) {
					zTree.expandNode(treeNodeP.children[i], false);
				}
			}
			while (pNode) {
				if (pNode === treeNode) {
					break;
				}
				pNode = pNode.getParentNode();
			}
			if (!pNode) {
				singlePath(treeNode);
			}
	
		}
		function singlePath(newNode) {
			if (newNode === curExpandNode) return;
			if (curExpandNode && curExpandNode.open==true) {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				if (newNode.parentTId === curExpandNode.parentTId) {
					zTree.expandNode(curExpandNode, false);
				} else {
					var newParents = [];
					while (newNode) {
						newNode = newNode.getParentNode();
						if (newNode === curExpandNode) {
							newParents = null;
							break;
						} else if (newNode) {
							newParents.push(newNode);
						}
					}
					if (newParents!=null) {
						var oldNode = curExpandNode;
						var oldParents = [];
						while (oldNode) {
							oldNode = oldNode.getParentNode();
							if (oldNode) {
								oldParents.push(oldNode);
							}
						}
						if (newParents.length>0) {
							for (var i = Math.min(newParents.length, oldParents.length)-1; i>=0; i--) {
								if (newParents[i] !== oldParents[i]) {
									zTree.expandNode(oldParents[i], false);
									break;
								}
							}
						} else {
							zTree.expandNode(oldParents[oldParents.length-1], false);
						}
					}
				}
			}
			curExpandNode = newNode;
		}
	
		function onExpand(event, treeId, treeNode) {
			curExpandNode = treeNode;
		}
		/**end*/
		
		function sub_mit(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			var type = zTree.setting.check.chkboxType;
			var checkedDept = new Array();
			var nodes = zTree.getCheckedNodes(true);
			var checkedIncludeChildren = new Array();
			var nameMap = new Map();
			if(type.Y=="ps"||type.Y=="s"){
				/**
				 * 需要子关联的情况,先判断哪些是父节点，但是还没有ajax展开的，这个
				 * 放在数组checkedIncludeChildren中，传到后台遍历出所有子节点。
				 * 其他的节点不需要再子关联，直接放到checkedDept中
				 */
				$.each(nodes,function(i,node){
					if(node.deep!="0"){
						//有子节点，但是没有ajax展开
						if(node.isParent==true&&node.children===undefined){
							checkedIncludeChildren[checkedIncludeChildren.length]=node.deptId;
							nameMap.put(node.deptId,node.name+"(包括下级部门)");
						}else{
							checkedDept[checkedDept.length]=node.deptId;
							nameMap.put(node.deptId,node.name);
						}
					}
				});
			}else{
				$.each(nodes,function(i,node){
					if(node.deep!="0"){
						//不需要子关联的节点，直接传到后台，遍历人员即可
						checkedDept[checkedDept.length]=node.deptId;
						nameMap.put(node.deptId,node.name);
					}
				});
			}
			//设置父页面的值
			var origin = artDialog.open.origin;
			origin.setDept(nameMap);
			origin.document.getElementById('checkedId').value=checkedDept.join(",");
			origin.document.getElementById('checkedIncludeChildren').value=checkedIncludeChildren.join(",");
			art.dialog.close();
		}
		
</script>
<style type="text/css">
	body{scrollbar-3dlight-color:#BDCFE4;scrollbar-arrow-color:#BDCFE4;scrollbar-face-color:#fff;scrollbar-shadow-color:#fff;scrollbar-darkshadow-color:#BDCFE4;}
</style>
</head>
<body style="margin:0px;width:250px;height:400px;">
	<div class="content_wrap" style="height:400px;width:250px;margin:0px;padding:0px;overflow-x:hidden;float:left;overflow-y:auto;background-color:#f2f2f2;">
		<p style="line-height:20px;margin:0px;color:#333333;font-weight:700;font-size:12px;font-family:verdana;">
			勾选时:<input type="checkbox" id="py" checked="checked"/><span>父关联</span>
			<input type="checkbox" id="sy" checked="checked"/><span>子关联</span><br/>
		</p>
		<div class="zTreeDemoBackground left">
			<ul id="treeDemo" class="ztree" style="width:370px;"></ul>
		</div>
	</div>
</body>
</html>